<?php

class Admin_ChuDeController extends Zend_Controller_Action
{
	public function init()
	{
		$this->_helper->layout->setLayout('admin');
	}
	
	public function indexAction()
	{
		$chudeMapper = Library_Model_ChuDeMapper::getInstance();
		$chude = $chudeMapper->chudeList();
		$this->view->chude = $chude;
		
		
	}
	
	public function themAction()
	{
		$this->view->title = 'Thêm mới chủ đề';
		$formThem = new Admin_Form_ChuDeForm();
		
		$formThem->setMethod('post');
		$formThem->setAction('/zfMHB/public/admin/chude/them');
		
		$formThem->addElement('submit', 'submit', array('label' => 'Thêm'));
		$this->view->form = $formThem;
		
		$formThem->getElement('chude')->addValidator('Db_NoRecordExists',true, array(
				'table' => 'tbl_chude',
				'field' => 'TenChuDe'
		));
		$formThem->getElement('chude')->getValidator('Db_NoRecordExists')->setMessage('Phòng ban đã tồn tại!');
		
		if($this->getRequest()->isPOST())
		{
			if($formThem->isValid($_POST))
			{
				$data = $formThem->getValues();
				$tenchude = $data['chude'];
				$chudeMapper = Library_Model_ChuDeMapper::getInstance();
				
				$chudeObj = new Library_Model_ChuDe();
				$chudeObj->setTenChuDe($tenchude);
				
				$chude = $chudeMapper->save($chudeObj);
				$this->_redirect('/admin/chude/index');
			}
		}
	}
	
	public function capnhatAction()
    {
        $this->view->title = 'Cập nhật chủ đề';
        $formCapnhat = new Admin_Form_ChuDeForm();        
        $formCapnhat->setMethod('post');
        $formCapnhat->setAction('/zfMHB/public/admin/chude/capnhat/');
        
        $formCapnhat->addElement('submit', 'submit', array('label' => 'Lưu'));
        $formCapnhat->addElement('hidden', 'id');
        $machude = $this->getRequest()->getParam('id');

        $formCapnhat->getElement('chude')->addValidator('Db_NoRecordExists', true, array(
        		'table' => 'tbl_chude',
        		'field' => 'TenChuDe',
        		'exclude' => array(
        				'field' => 'MaChuDe',
        				'value' => $machude)
        ));
        
     	$formCapnhat->getElement('chude')->getValidator('Db_NoRecordExists')->setMessage('Chủ đề đã tồn tại!');
        $chudeMapper = Library_Model_ChuDeMapper::getInstance();
        $this->view->form = $formCapnhat;
        
       if($this->getRequest()->isPOST())
		{
			if($formCapnhat->isValid($_POST))
			{
				$data = $formCapnhat->getValues();
				
				$id =	$data['id'];
				$tenchude = $data['chude'];
				
				$chudeObj = new Library_Model_ChuDe();
				
				$chudeObj->setId($id);
				$chudeObj->setTenChuDe($tenchude);
				
				$chude = $chudeMapper->save($chudeObj);
				$this->_redirect('/admin/chude/index');
				
			}
		}
		else
		{
			$chude = $chudeMapper->searchChuDeById($machude);

			$formCapnhat->getElement('id')->setValue($chude->getID());
			$formCapnhat->getElement('chude')->setValue($chude->getTenchude());
		}
	}
	
	public function xoaAction()
	{
		$this->_helper->viewRenderer->setNoRender(true);
		$mapb = $this->getRequest()->getParam('id');
		$chudeMapper = Library_Model_ChuDeMapper::getInstance();
		$chudeMapper->delete($mapb);
		$this->_redirect('/admin/chude/index');
		
	}
}